/**
 * Created by Celebration-Grey on 2017/4/25.
 */
weibo.public.hoverShow = function($hoverobj, $showobj) {
    var timer;
    $hoverobj.hover(function() {
        $showobj.fadeIn();
    });
    $hoverobj.mouseleave(function() {
        timer = setTimeout(function() {
            $showobj.fadeOut();
        }, 500);
        $showobj.mouseenter(function() {
            clearTimeout(timer);
        });
        $hoverobj.mouseenter(function() {
            clearTimeout(timer);
        });
    });
    $showobj.mouseleave(function() {
        timer = setTimeout(function() {
            $showobj.fadeOut();
        }, 500);
        $showobj.mouseenter(function() {
            clearTimeout(timer);
        });
        $hoverobj.mouseenter(function() {
            clearTimeout(timer);
        });
    });
};

weibo.public.hoverShows = function ($face, $card, fnPostition) {
    var timer;
    var timer1;
    $face.unbind('mouseleave');
    $face.mouseleave(function () {
        timer = setTimeout(function () {
            $card.remove();
            $face.unbind('mouseenter');
            $face.mouseenter(function () {
                 var pos = $(this).offset();
                timer1 = setTimeout(function () {
                    fnPostition();
                    weibo.bowen.hoverShow($face, $card, fnPostition);
                    $('body').append($card);
                }, 500);
                $face.mouseleave(function () {
                    clearTimeout(timer1);
                });
            });
        }, 1000);
        $card.unbind('mouseenter');
        $card.mouseenter(function () {
            clearTimeout(timer);
        });
        $face.unbind('mouseenter');
        $face.mouseenter(function () {
            clearTimeout(timer);
        });
    });
    $card.unbind('mouseleave');
    $card.mouseleave(function () {
        timer = setTimeout(function () {
            $card.remove();
            $face.unbind('mouseenter');
            $face.mouseenter(function () {
                var pos = $(this).offset();
                timer1 = setTimeout(function () {
                    fnPostition();
                    weibo.bowen.hoverShow($face, $card, fnPostition);
                    $('body').append($card);
                }, 500);
                $face.mouseleave(function () {
                    clearTimeout(timer1);
                });
            });
        }, 1000);
        $card.unbind('mouseenter');
        $card.mouseenter(function () {
            clearTimeout(timer);
        });
        $face.unbind('mouseenter');
        $face.mouseenter(function () {
            clearTimeout(timer);
        });
    });
};

weibo.public.logOutRequest = function () {
    $.ajax({
        type: 'GET',
        url: host + 'weibo_logOut',
        data: {},
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.status == 200) {
                localStorage.clear();
                window.location.href = 'login.html';
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    })
};

weibo.public.logOut = function () {
    weibo.public.logOutRequest();
};

weibo.public.getQueryString = function(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
};

weibo.public.followAction = function (uid,fnSuccess) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_follow',
        data: {
            uid: uid
        },
        xhrFields: {
            withCredentials: true
        },
        success: fnSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.public.unfollowAction = function (uid,fnSuccess) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_unfollow',
        data: {
            uid: uid
        },
        xhrFields: {
            withCredentials: true
        },
        success: fnSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.public.htmlEncodeByRegExp = function (str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&amp;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    return s;
};

weibo.public.htmlDecodeByRegExp = function (str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
};

!function($) {
    $.Huimodalalert = function(info, speed) {
        if ($(".modal-alert").length > 0) {
            $(".modal-alert").remove();
        }
        if (speed == 0 || typeof(speed) == "undefined") {
            $(document.body).append('<div id="modal-alert" class="modal modal-alert radius">' + '<div class="modal-alert-info">' + info + '</div>' + '<div class="modal-footer"> <button class="btn btn-primary radius" onClick="$.Huimodal_alert.hide()">确定</button></div>' + '</div>');
            $("#modal-alert").fadeIn();
        } else {
            $(document.body).append('<div id="modal-alert" class="modal modal-alert radius">' + '<div class="modal-alert-info">' + info + '</div>' + '</div>');
            $("#modal-alert").fadeIn();
            setTimeout($.Huimodalalert.hide, speed);
        }
    }
    $.Huimodalalert.hide = function() {
        $("#modal-alert").fadeOut("normal",
            function() {
                $("#modal-alert").remove();
            });
    }
} (window.jQuery);